package com.ssy.lingxi.pay.serviceimpl;

import cn.hutool.core.date.DateUtil;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.pay.common.CreditApplySuperiorInnerStatusEnum;
import com.ssy.lingxi.pay.entity.CreditInnerVerifyRecordDO;
import com.ssy.lingxi.pay.model.vo.response.CreditInnerVerifyRecordVO;
import com.ssy.lingxi.pay.repository.CreditInnerVerifyRecordRepository;
import com.ssy.lingxi.pay.service.ICreditInnerVerifyRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CreditInnerVerifyRecordServiceImpl implements ICreditInnerVerifyRecordService {

    @Resource
    private CreditInnerVerifyRecordRepository recordRepository;

    /**
     * 获取审核历史记录
     * @author 伍将
     * @date 2020/8/20
     * @param applyId:
     * @return com.ssy.lingxi.common.response.Wrapper<java.util.List < com.ssy.lingxi.pay.model.vo.response.CreditInnerVerifyRecordVO>>
     **/
    @Override
    public Wrapper<List<CreditInnerVerifyRecordVO>> getHistoryList(Long applyId) {

        // step 1: 分页查询数据
        List<CreditInnerVerifyRecordDO> recordDOList = recordRepository.findByApplyIdOrderByOperateTimeAsc(applyId);

        // step 2: 组装返回数据
        List<CreditInnerVerifyRecordVO> resultList = recordDOList.stream().map(r -> {
            CreditInnerVerifyRecordVO queryVO = new CreditInnerVerifyRecordVO();
            queryVO.setStep(r.getStep());
            queryVO.setOperator(r.getOperator());
            queryVO.setDepartment(r.getDepartment());
            queryVO.setJobTitle(r.getJobTitle());
            queryVO.setStatus(r.getStatus());
            queryVO.setOperate(r.getOperate());
            queryVO.setOperateTime(DateUtil.format(DateUtil.date(r.getOperateTime()), "yyyy-MM-dd HH:mm"));
            queryVO.setOpinion(r.getOpinion());
            queryVO.setStatusName(CreditApplySuperiorInnerStatusEnum.getItemMessage(r.getStatus()));
            return queryVO;
        }).collect(Collectors.toList());

        return Wrapper.success(resultList);
    }
}
